############################################################################################
#                               Fonctions de gestion des Jeux                              #
############################################################################################

# Requête permettant d'ajouter un jeu
sub addGame{#$name->get,$date->get,$price->get,$nbMaxPlayer->get,$nbMinPlayer->get,$condition
  my $req = $dbd->prepare("INSERT INTO jeu (nomJeu,etat,dateAchat,prixAchat,joueursMin,joueursMax) VALUES ('$_[0]', '$_[5]', str_to_date('$_[1]','%d/%m/%Y'), '$_[2]', '$_[3]', '$_[4]');");
  $req->execute;
  $req->finish; 
}

# Requête permettant de supprimer un jeu
sub deleteGame{#$name->get,$date->get,$price->get,$nbMaxPlayer->get,$nbMinPlayer->get,$condition
  my $req = $dbd->prepare("DELETE FROM jeu WHERE nomJeu = '$_[0]';");
  $req->execute;
  $req->finish;
}

# Requête permettant de rechercher des jeux
sub searchGame{#$name->get,$date->get,$price->get,$nbMaxPlayer->get,$nbMinPlayer->get,$condition
  my @quotedArgs;
  my $i=0;
  foreach my $arg (@_) {
    $quotedArgs[$i++]=quotemeta($arg);
  }

  my $date = convertDate($_[1]) . '%';

  my $minPlayer = 0;
  my $maxPlayer = 100000;

  if($_[4] ne ''){
    $minPlayer = $_[4];
  }

  if($_[3] ne ''){
    $maxPlayer = $_[3];
  }

  my $req;
  $req = <<"SQL";
  SELECT * from jeu
    where jeu.nomJeu like '%$quotedArgs[0]%'
      and jeu.dateAchat like '$date'
      and jeu.prixAchat like '$_[2]%'
      and jeu.etat like '$quotedArgs[5]%'
      and jeu.joueursMin >= '$minPlayer'
      and jeu.joueursMax <= '$maxPlayer'
      order by jeu.nomJeu;
SQL
  my $hashGames = $dbd->selectall_hashref($req, 'nomJeu');
  return $hashGames;
}

# Requête permettant de mettre à jour les informations d'un jeu
sub updateGame {#$name->get,$date->get,$price->get,$nbMaxPlayer->get,$nbMinPlayer->get,$condition
  my @quotedArgs;
  my $i=0;
  foreach my $arg (@_) {
    $quotedArgs[$i++]=quotemeta($arg);
  }
 
  my $date = convertDate($_[1]);
  my $req = <<"SQL";
    UPDATE jeu
        SET dateAchat = '$date',
            prixAchat = '$_[2]',
            etat = '$_[5]',
            joueursMin = '$_[4]',
            joueursMax = '$_[3]'
        WHERE nomJeu = '$_[0]';
SQL
  my $prep = $dbd->prepare($req);
  $prep->execute;
  $prep->finish;
}

# Requête permettant de récupérer les notes d'un jeu
sub getMarkOfGame{
  my $hashGames = $dbd->selectall_hashref("SELECT * from note,adherent WHERE note.nomJeu = '$_[0]' AND note.idAdherent = adherent.idAdherent;", 'idAdherent');
  return $hashGames;
}

# Requête permettant de noter un jeu
sub markGame{
  my @quotedArgs;
  my $i=0;
  foreach my $arg (@_) {
    $quotedArgs[$i++]=quotemeta($arg);
  }

  my $req = $dbd->prepare("INSERT INTO note (nomJeu,idAdherent,commentaire,note) VALUES ('$quotedArgs[0]', $_[1], '$quotedArgs[2]', '$_[3]');");
  $req->execute;
  $req->finish; 
}

1;